﻿namespace SQLPXBase.Factory.ChangeAwareness
{
    public abstract class ChangeAwareBaseModel<PX>:FactorizableModel<PX>, IChangingModel 
        where PX : Interfaces.ISQLProxyBase, new()
    {
        private ChangingModelHash modelHash = null;

        protected ChangeAwareBaseModel(ModelFactory<PX> factory)
            : base(factory)
        {
        }

        #region Implementation of IChangingModel
        
        public virtual void FreezeForComparison()
        {
            //modelHash = ChangingModelHash.StandardModelSerialize(this);
            modelHash = this._FreezeForComparison();
        }

        public virtual bool HasModelChangedSinceFreeze()
        {
            if (modelHash == null) return true;
            //ChangingModelHash tmp = ChangingModelHash.StandardModelSerialize(this);
            //return ChangingModelHash.CheckForChanges(modelHash, tmp);

            return this._HasModelChangedSinceFreeze(modelHash);
        }

        #endregion
    }

    public static class ChangeAware
    {
        public static ChangingModelHash _FreezeForComparison<PX>(this FactorizableModel<PX> model) 
            where PX : Interfaces.ISQLProxyBase, new()
        {
            return ChangingModelHash.StandardModelSerialize(model);
        }

        public static bool _HasModelChangedSinceFreeze<PX>(this FactorizableModel<PX> model, ChangingModelHash modelHash) 
            where PX : Interfaces.ISQLProxyBase, new()
        {
            if (modelHash == null) return true;
            ChangingModelHash tmp = ChangingModelHash.StandardModelSerialize(model);
            return ChangingModelHash.CheckForChanges(modelHash, tmp);
        }
    }
}
